package fr.laposte.erreurcaisse.modeles;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.laposte.erreurcaisse.DAO.DAO_Factory;
import fr.laposte.erreurcaisse.DAO.DAO_Interface;
import fr.laposte.erreurcaisse.beans.*;

/**
 * Servlet implementation class Auth
 */
public class Auth extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Auth() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
			{
		I_Bean b = null;
		HttpSession session = request.getSession();	
		if ((request.getParameter("username") != "")&&(request.getParameter("Password") != ""))
		{
			Authentification Agent = new Authentification();
			Agent.setAgentCode(request.getParameter("username"));
			Agent.setPass(request.getParameter("Password"));
			DAO_Interface dao = DAO_Factory.getDao(Agent);
			b = dao.find();
			
			if ((b != null) && ((Authentification) b).getPass() == Agent.getPass())
			{
			session.setAttribute("agent", Agent);
			this.getServletContext().getRequestDispatcher("/WEB-INF/agentguichet-Welcome.jsp").forward(request, response);
			}
			else
			{
				this.getServletContext().getRequestDispatcher("/WEB-INF/index.jsp")
				.forward(request, response);
			}
		} else {
			this.getServletContext().getRequestDispatcher("/WEB-INF/index.jsp")
					.forward(request, response);
		}
	}

}
